<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class storyreadController extends AppController
{
    public $class = 'storyread';
    public function __construct() {
        parent::__construct();
        $this->modelObj = new storyContentModel();
        $this->log_type = '预览管理';
        $this->title = $this->log_type;
        $this->cookie_key = 'story_read_cookie';
        $this->assign('class', $this->class);
    }
    /**
     * 阅读
     */
    public function view() {
        $this->title = '预览稿子';
        $this->_get_data();
        $tid = isset($_REQUEST['tid']) ? $_REQUEST['tid'] : 0;        
        $tname = storyTitleModel::load()->getName($tid);
        $this->assign('tid', $tid);
        $this->assign('title_name', $tname);
        $this->display('read_list');
    }
    public function query(){
        $this->_get_data();
        $this->ret['status'] = 0;
        $this->ret['content'] = $this->fetch('read_list');
        exit(json_encode($this->ret));
    }
    public function _get_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;  
        $filter['tid'] = isset($_REQUEST['tid']) ? $_REQUEST['tid'] : 0;
        if($filter['tid'] > 0) {
            $where .= ' and tid = '. $filter['tid'];
        }
        $key = $this->modelObj->keyFields;
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $key;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'ASC';
        $pageObj = new pageModel('story_content', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);
        $list = $data['pageData'];
        //替换 javascript:listTable.showPage
        $data['pageHtml'] = str_replace('listTable.showPage', 'pageNext', $data['pageHtml']);        
        if($list) {
            foreach ($list as $key => $row) {
                $data['pageData'][$key]['id'] = $key + 1;
                $tname = storyTitleModel::load()->getName($row['tid']);
                $data['pageData'][$key]['tname'] = $tname;
                $data['pageData'][$key]['floor'] = $row['floor'] == 0 ? '楼主' : $row['floor'] .'楼';
                $data['pageData'][$key]['floorNum'] = $row['floor'] == 0 ? '' : $row['floor'];
                $data['pageData'][$key]['in_time_format'] = publicController::load()->friendlyDate($row['in_time']);       
                $data['pageData'][$key]['restime_format'] = publicController::load()->friendlyDate($row['restime']);
                $data['pageData'][$key]['is_show'] = $row['is_show'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';  
            }
        }
        $this->assign('data', $data);
    }
    /**
     * 获取内容ajax
     */
    public function get() {
        $cid = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if($cid > 0) {
            $row = storyContentModel::load()->get_info($cid);
            if($row) {
                exit($this->setReturn(0, $row));
            }
        }
        exit($this->setReturn(100, 'error'));
    }
    /**
     * 删除
     */
    public function del(){
        $cid = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if($cid > 0) {
            $where = array(
                'cid' => $cid,
            );
            $bool = storyContentModel::load()->delete($where);
            if($bool) {
                exit($this->setReturn(0, 'ok'));
            }
        }
        exit($this->setReturn(100, 'error'));
    }
    /**
     *  更新content
     */
    public function save(){
        $cid = intval($_POST['cid']);
        if($cid > 0) {
            $content = $_POST['content'];
            $bool = storyContentModel::load()->updateContent($cid, $content);
            if($bool) {
                exit($this->setReturn(0, 'ok'));
            }
        }
        exit($this->setReturn(10, 'error'));
    }
}